package www.bplead.com.mydemo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import www.bplead.com.controller.action.user.entity.UserInfo;
import www.bplead.com.mydemo.R;

/**
 * Created by wangyangzi on 2016/9/5.
 */
public class LoadImageAdapter extends BaseAdapter{
    private Context context;
    private List<UserInfo> userList;

    public LoadImageAdapter(Context context,List<UserInfo> userList){
        this.context = context;
        this.userList = userList;
    }

    @Override
    public int getCount() {
        return userList.size();
    }

    @Override
    public Object getItem(int position) {
        return userList.get(position);
    }

    @Override
    public long getItemId(int position) {
        if(userList.get(position).id == null){
            return 0;
        }
        return userList.get(position).id;
    }

    DisplayImageOptions options = null;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        UserInfo userInfo = (UserInfo) getItem(position);

        ViewHolder viewHolder = null;
        //显示图片的配置

        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.user_info,null);
            viewHolder = new ViewHolder();
            viewHolder.postraitIV = (ImageView) convertView.findViewById(R.id.user_postrait_iv);
            viewHolder.nameTV = (TextView) convertView.findViewById(R.id.username_tv);

            options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.drawable.loading)
                    .showImageOnFail(R.drawable.error)
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .build();

            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ImageLoader.getInstance().displayImage(userInfo.postrait, viewHolder.postraitIV, options);
        viewHolder.nameTV.setText(userInfo.username);

        return convertView;
    }

    public static class ViewHolder{
        public ImageView postraitIV;
        public TextView nameTV;
    }
}
